What is @turf/distance?
@turf/distance is a module of the Turf.js library, which provides geospatial analysis tools. This specific module calculates the distance between two geographical points using various measurement units such as miles, kilometers, and degrees.
What are @turf/distance's main functionalities?
Calculate Distance in Kilometers
This feature calculates the distance between two geographical points in kilometers. The 'from' and 'to' variables represent the coordinates of the two points.
const turf = require('@turf/distance');
const from = [0, 0];
const to = [1, 1];
const options = { units: 'kilometers' };
const distance = turf(from, to, options);
console.log(distance);
Calculate Distance in Miles
This feature calculates the distance between two geographical points in miles. The 'from' and 'to' variables represent the coordinates of the two points.
const turf = require('@turf/distance');
const from = [0, 0];
const to = [1, 1];
const options = { units: 'miles' };
const distance = turf(from, to, options);
console.log(distance);
Calculate Distance in Degrees
This feature calculates the distance between two geographical points in degrees. The 'from' and 'to' variables represent the coordinates of the two points.
const turf = require('@turf/distance');
const from = [0, 0];
const to = [1, 1];
const options = { units: 'degrees' };
const distance = turf(from, to, options);
console.log(distance);
Other packages similar to @turf/distance
geolib
Geolib is a library to provide basic geospatial operations like distance calculation, area calculation, and more. It offers similar functionalities to @turf/distance but also includes additional features like finding the center of a cluster of points.
geodesy
Geodesy is a library for geodesic calculations, including distance and bearing between points, and conversions between different coordinate systems. It provides more advanced geospatial calculations compared to @turf/distance.
spherical-geometry-js
Spherical Geometry is a library for performing spherical geometry calculations, including distance and area calculations on the surface of a sphere. It is similar to @turf/distance but focuses on spherical geometry.
@turf/distance
distance
Calculates the distance between two points in degrees, radians, miles, or kilometers.
This uses the Haversine formula to account for global curvature.
Parameters
from
Coord origin pointto
Coord destination pointoptions
Object Optional parameters (optional, default {}
)
options.units
string can be degrees, radians, miles, or kilometers (optional, default 'kilometers'
)
Examples
var from = turf.point([-75.343, 39.984]);
var to = turf.point([-75.534, 39.123]);
var options = {units: 'miles'};
var distance = turf.distance(from, to, options);
var addToMap = [from, to];
from.properties.distance = distance;
to.properties.distance = distance;
Returns number distance between the two points
This module is part of the Turfjs project, an open source
module collection dedicated to geographic algorithms. It is maintained in the
Turfjs/turf repository, where you can create
PRs and issues.
Installation
Install this module individually:
$ npm install @turf/distance
Or install the Turf module that includes it as a function:
$ npm install @turf/turf